import { createRouter, createWebHistory } from "vue-router";
import type { RouteRecordRaw } from "vue-router";
import {userInfo} from "@/store/userInfo.ts";

const routes:Array<RouteRecordRaw>= [
  {
    path: "/",
    component: () => import("@/views/home/home.vue"),
    children: []
  },
  {
    path: "/login",
    component: () => import("@/views/login/login.vue")
  },
  {
    path: "/:catchAll(.*)",
    redirect: "/login",
  }
];

export const router = createRouter({
  history: createWebHistory(),
  routes,
});

const noTokenPath = ['/login'];
// 忽略检查代码，让ts跳过检查这行代码
/* @ts-ignore */
router.beforeEach((to, from, next) => {
  if(noTokenPath.includes(to.path) || userInfo().token) {
    next();
  }else{
    next('/login');
  }
})